package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import cn.com.sinux.spring.json.JsonFilter;
import cn.com.sinux.spring.utils.BeanUtils;
import cn.com.sinux.spring.utils.PageUtils;
import com.sinosoft.hydra.um.condition.SearchDeviceCondition;
import com.sinosoft.hydra.um.domain.model.LabDataParser;
import com.sinosoft.hydra.um.domain.permission.TDevice;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.DeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 设备管理api
 */
@Controller
@RequestMapping("api/device")
public class SysDeviceApi {

    @Autowired
    private DeviceService deviceService;

    /**
     * 添加设备
     *
     * @param device 设备
     * @return 设备保存结果
     * @throws Exception
     */
    @RequestMapping(value = "addDevice", method = RequestMethod.POST)
    @RequiresPermissions(value = "device:data:add")
    @ResponseBody
    public Object addDevice(TDevice device) throws Exception {
        TDevice tDevice = deviceService.addDevice(device);
        LoggerHelper.logInfo("添加了设备‘{}’", tDevice.getDeviceName());
        return JsonFilter.doPropertyFilter(tDevice, new PropertyCollection()
                .addProperties(TDevice.class, "id", "deviceName"));
    }

    /**
     * 修改设备
     *
     * @param device 设备
     * @return 设备保存结果
     * @throws Exception
     */
    @RequestMapping(value = "updateDevice", method = RequestMethod.POST)
    @RequiresPermissions(value = "device:data:update")
    @ResponseBody
    public Object updateDevice(TDevice device) throws Exception {
        TDevice db = deviceService.findOneDevice(device.getId());
        TDevice source = new TDevice();
        BeanUtils.copyProperties(db, source);
        TDevice tDevice = deviceService.updateDevice(device);
        LoggerHelper.logInfo("修改了设备‘{}’{}", tDevice.getDeviceName(), new LoggerHelper.PropertyMerge(source, tDevice)
                        .addPropertyDes("deviceName", "设备名称")
                        .addPropertyDes("deviceCode", "设备编码")
                        .addPropertyDes("manufacturer", "生产厂家")
                        .addPropertyDes("purchaseDate", "购买日期")
                        .addPropertyDes("purchaseAmount", "购买金额")
                        .addPropertyDes("keyParameter", "主要参数")
                        .addPropertyDes("repairRecord", "维修记录")
                        .addPropertyDes("operators", "操作人员")
                        .addPropertyDes("deviceCover", "设备封面")
                        .addPropertyDes("parserDevice", "是否程控设备")
        );
        return JsonFilter.doPropertyFilter(tDevice, new PropertyCollection()
                .addProperties(TDevice.class, "id"));
    }

    /**
     * 修改设备
     *
     * @param searchDeviceCondition 删除设备的ids集合
     * @return 删除结果
     * @throws Exception
     */
    @RequestMapping(value = "deleteDevices", method = RequestMethod.POST)
    @RequiresPermissions(value = "device:data:delete")
    @ResponseBody
    public Object deleteDevices(SearchDeviceCondition searchDeviceCondition) throws Exception {
        List<TDevice> devices = deviceService.deleteDevices(searchDeviceCondition.getIds());
        LoggerHelper.logInfo("删除了设备‘{}’", new LoggerHelper.PropertyReader<TDevice>(devices, "deviceName"));
        return JsonFilter.doPropertyFilter(devices, new PropertyCollection()
                .addProperties(TDevice.class, "id", "deviceName"));
    }

    /**
     * 搜索设备
     *
     * @param searchDeviceCondition 搜索条件
     * @return 搜索结果
     * @throws Exception
     */
    @RequestMapping(value = "findDevices")
    @RequiresPermissions(value = "device:data:list")
    @ResponseBody
    public Object findDevices(
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer pageNumber,
            @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize,
            SearchDeviceCondition searchDeviceCondition) throws Exception {
        Pageable page = PageUtils.buildPageRequest(pageNumber, pageSize, "id", "desc");
        Page<TDevice> devicePage = deviceService.findDevices(searchDeviceCondition, page);
        return JsonFilter.doPropertyFilter(devicePage, new PropertyCollection()
                .addProperties(TDevice.class, "id", "deviceCode", "deviceName", "manufacturer", "purchaseDate",
                        "purchaseAmount", "keyParameter", "repairRecord", "operators", "parserDevice", "deviceCover"));
    }

    /**
     * 设备json数据查询
     *
     * @param searchDeviceCondition 搜索条件
     * @return 搜索结果
     * @throws Exception
     */
    @RequestMapping(value = "list")
    @RequiresPermissions(value = "device:data:list")
    @ResponseBody
    public Object list(
            SearchDeviceCondition searchDeviceCondition) throws Exception {
        Page<TDevice> devicePage = deviceService.findDevices(searchDeviceCondition, null);
        return JsonFilter.doPropertyFilter(devicePage, new PropertyCollection()
                .addProperties(TDevice.class, "id", "deviceCode", "deviceName", "deviceCover"));
    }

    /**
     * 修改设备页面
     *
     * @return String 页面名称
     */
    @RequestMapping("toUpdate")
    public String toUpdate(Model model,
                           @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("device", deviceService.findOneDevice(id));
        model.addAttribute("parsers", LabDataParser.values());
        return "hydra_easyui/device/updateDevice";
    }

    /**
     * 解析器搜索页面
     *
     * @return 搜索页
     */
    @RequestMapping("toSearch")
    @RequiresPermissions(value = "device:data:list")
    public String toSearch() {
        return "hydra_easyui/device/searchDevice";
    }

    /**
     * 新增设备页面
     *
     * @return 新增页面
     */
    @RequestMapping("toAdd")
    @RequiresPermissions(value = "device:data:add")
    public String toAdd(Model model) {
        model.addAttribute("parsers", LabDataParser.values());
        return "hydra_easyui/device/addDevice";
    }

    /**
     * 设备首页面
     *
     * @return 页面
     */
    @RequestMapping("index")
    @RequiresPermissions(value = "device:data:list")
    public String index() {
        LoggerHelper.logInfo("访问了设备列表");
        return "hydra_easyui/device/index";
    }
}
